package com.example.myanimator.scroll

import android.content.Context
import android.util.Log
import androidx.recyclerview.widget.RecyclerView
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import com.example.myanimator.R
import com.bumptech.glide.Glide
import com.example.myanimator.databinding.ActivityScrollerItemBinding

/**
 * @author tgw
 * @date 2021/11/22
 * @describe
 */
internal class RecycleViewImageListAdapter(
    private val mContext: Context
) : MyListAdapter<String, RecycleViewImageListAdapter.ImageViewHolder>(ImageViewListAdapterCallback()) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ImageViewHolder {
        val binding: ActivityScrollerItemBinding = DataBindingUtil.inflate(
            LayoutInflater.from(mContext),
            R.layout.activity_scroller_item,
            parent,
            false
        )
        return ImageViewHolder(binding.root)
    }

    override fun onBindViewHolder(holder: ImageViewHolder, position: Int) {
        val photo = getItem(position % mDiffer.currentList.size)
        Log.d("tgw", "onBindViewHolder: $position---$itemCount")
        Glide.with(mContext).load(photo).into(holder.imageView)
    }


    override fun getItemCount(): Int {
        return Int.MAX_VALUE
    }

    internal class ImageViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val imageView: ImageView

        init {
            imageView = itemView.findViewById(R.id.image)
        }
    }

    internal class ImageViewListAdapterCallback : DiffUtil.ItemCallback<String>() {
        override fun areItemsTheSame(oldItem: String, newItem: String): Boolean {
            return oldItem == newItem
        }

        override fun areContentsTheSame(oldItem: String, newItem: String): Boolean {
            return oldItem == newItem
        }
    }

}